package com.example.saifly.mainPage.home;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.common.Application.MyApplication;
import com.example.common.Fragment;
import com.example.saifly.Pop.PopNursingActivity;
import com.example.saifly.R;
import com.example.saifly.inPageMy.AboutActivity;
import com.example.saifly.inPageMy.CollectionActivity;
import com.example.saifly.inPageMy.DeviceActivity;
import com.example.saifly.inPageMy.DoctorAdviceActivity;
import com.example.saifly.inPageMy.HomeSetActivity;
import com.example.saifly.inPageMy.UserAdviceActivity;
import com.example.saifly.inPageMy.UserQuestionActivity;
import com.example.saifly.inPageRecord.EnvironmentActivity;

import de.hdodenhof.circleimageview.CircleImageView;

import static android.content.Context.MODE_PRIVATE;


public class HomeFragment extends Fragment
{
    private LinearLayout llDoctorAdvice;
    private LinearLayout llCollection;
    private LinearLayout llDevice;
    private LinearLayout llUserQuestion;
    private LinearLayout llAbout;
    private LinearLayout llUserAdvice;
    private ImageView ivSet;
    private CircleImageView ivPortrait;   //头像
    private TextView tvNickName;   //用户名

    String nickname;  //用户名
    String imageUrl;   //头像路径

    //初始化全局变量，稍后用于存入uid
    SharedPreferences sharedPreferences= MyApplication.getContext().getSharedPreferences("config",Context.MODE_PRIVATE);

    @Override
    protected int getContentLayoutId()
    {
        return R.layout.fragment_home;
    }

    @Override
    protected void init()
    {
        super.init();

        llDoctorAdvice=getActivity().findViewById(R.id.home_doctor_advice);
        llCollection=getActivity().findViewById(R.id.home_collection);
        llDevice=getActivity().findViewById(R.id.home_device);
        llUserQuestion=getActivity().findViewById(R.id.home_question);
        llAbout=getActivity().findViewById(R.id.home_about);
        llUserAdvice = getActivity().findViewById(R.id.home_user_advice);
        ivSet = getActivity().findViewById(R.id.home_set);
        ivPortrait=getActivity().findViewById(R.id.home_avatar);
        tvNickName=getActivity().findViewById(R.id.home_userName);
        doJump();

        getData();  //获取本地数据

        setData();  //加载用户名和头像
    }

    @Override
    public void onStart() {
        super.onStart();
        getData();
        setData();
    }

    private void doJump()
    {
        //llDoctorAdvice.setOnClickListener(v -> startActivity(new Intent(getActivity(), PopNursingActivity.class)));
        llDoctorAdvice.setOnClickListener(v -> startActivity(new Intent(getActivity(), DoctorAdviceActivity.class)));
        //llDoctorAdvice.setOnClickListener(v -> startActivity(new Intent(getActivity(), EnvironmentActivity.class)));
        llCollection.setOnClickListener(v -> startActivity(new Intent(getActivity(), CollectionActivity.class)));
        llDevice.setOnClickListener(v -> startActivity(new Intent(getActivity(), DeviceActivity.class)));
        llUserQuestion.setOnClickListener(v -> startActivity(new Intent(getActivity(), UserQuestionActivity.class)));
        llAbout.setOnClickListener(v -> startActivity(new Intent(getActivity(), AboutActivity.class)));
        llUserAdvice.setOnClickListener(v -> startActivity(new Intent(getActivity(), UserAdviceActivity.class)));
        ivSet.setOnClickListener(v -> startActivity(new Intent(getActivity(), HomeSetActivity.class)));
    }

    private void getData()  //从本地获取头像和用户名
    {
        nickname = sharedPreferences.getString("nickname", "default");
        imageUrl = sharedPreferences.getString("portrait","default");
        Log.d("头像", "logData: "+sharedPreferences.getString("portrait","default"));
    }

    private void setData()   //显示获取到的头像和用户名
    {
        Glide.with(HomeFragment.this).load("https://lztoos.oss-cn-shenzhen.aliyuncs.com/"+imageUrl).into(ivPortrait);
        tvNickName.setText(nickname);
    }


}